﻿using Microsoft.EntityFrameworkCore;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace PowerHub.MES.Repository.Basic
{
    /// <summary>
    /// 用户数据接口实现层
    /// </summary>
    public class BasicDataUserinfoRepository : BaseRepository<BasicData_Userinfo>, IBasicDataUserinfoRepository
    {
        private PowerHubMesContext _repositorySys;

        public BasicDataUserinfoRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }



        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<OperationPageResult<List<BasicData_Userinfo>>> GetPageListAsync(QueryBasicData_UserinfoDTO query)
        {
            var result = new OperationPageResult<List<BasicData_Userinfo>>();

            var currentQuery = GetEntitys()
                                  .AsNoTracking()
                                 .Include(p => p.Department)
                                 .Include(p => p.Roleinfo)
                                 .Where(p => p.IsDel == 0)
                                 .Where(p => p.Department.DepartmentName.Contains(query.DepartmentName ?? string.Empty))
                                 .Where(u=>u.UserName.Contains(query.UserName ?? string.Empty))
                                 .Where(u=>u.JobNumber.Contains(query.JobNumber ?? string.Empty));

            result.PageIndex = query.PageIndex;
            result.PageSize = query.PageSize;
            result.TotalCount = await currentQuery.CountAsync();

            if (result.TotalCount > 0)
            {
                result.Data = await currentQuery.OrderBy(g => g.JobNumber).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> UpdateBasicData_UserinfoAsyncRepository(BasicData_Userinfo query)
        {
            return UpdataEntity(query);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> BatchDeletionBasicData_UserinfoAsyncRepository(List<string>? ids)
        {
            var result = new OperationResult<object>();

            using (var transaction = _repositorySys.Database.BeginTransaction())
            {
                try
                {
                    var departments = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
                    foreach (var item in departments)
                    {
                        item.IsDel = 1;
                        GetEntitys().Update(item);
                    }

                    _repositorySys.SaveChanges();
                    transaction.Commit();
                    result.Message = "删除成功";
                    result.Data = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }

            return result;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CreateBasicData_UserinfoAsyncRepository(BasicData_Userinfo query)
        {
            return AddEntity(query);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BasicData_Userinfo> GetBasicDataUserinfoSingleAsync(string Id)
        {
            return GetEntitySingle(Id);

        }
    }
}
